﻿#coding:utf_8

import os
import glob


Path=r"E:\02.ProjectSource\20130707.Midea.HFMUpgrade\HFMService"
Ext="*\\*.cs"
files = glob.glob(Path+ "\\" + Ext)

print "Hello"
for each_file in files:
    print each_file


codefiles = []
for dirname, dirnames, filenames in os.walk(Path):
    # print path to all subdirectories first.
    for subdirname in dirnames:
        #print os.path.join(dirname, subdirname)
        pass

    # print path to all filenames.
    for filename in filenames:
        if os.path.splitext(filename)[1] == ".cs":
            print os.path.join(dirname, filename)
            rf = open (os.path.join(dirname, filename), "rb")
            buf = rf.read(1024000)
            rf.close ()
            codefiles.append(buf)
codetext = '\n'.join (codefiles)
#print codetext


configs = []
for dirname, dirnames, filenames in os.walk(Path):
    # print path to all subdirectories first.
    for subdirname in dirnames:
        #print os.path.join(dirname, subdirname)
        pass

    # print path to all filenames.
    for filename in filenames:
        if os.path.splitext(filename)[1] == ".config":
            print os.path.join(dirname, filename)
            rf = open (os.path.join(dirname, filename), "rb")
            buf = rf.read(1024000)
            rf.close ()
            configs.append(buf)
configtext = '\n'.join (configs)
#print configtext

import re
keys = re.findall(r"""add key="(.+?)" value""", configtext)
keys = list(set(keys))
#print '\n'.join (keys)
#print "Done"

not_used_key = []
for each_key in keys:
    results = re.findall( each_key  , codetext)
    #print len(results), results
    if len(results) == 0:
        print each_key
        not_used_key.append (each_key)
print "---------- Done ----------"
                  


configs = []
for dirname, dirnames, filenames in os.walk(Path):
    # print path to all subdirectories first.
    for subdirname in dirnames:
        #print os.path.join(dirname, subdirname)
        pass

    # print path to all filenames.
    for filename in filenames:
        if os.path.splitext(filename)[1] == ".config":
            print os.path.join(dirname, filename)
            rf = open (os.path.join(dirname, filename), "rb")
            buf = rf.read(1024000)
            rf.close ()
            configs.append(buf)

            print filename
            for each_key in not_used_key:
                newbuf = buf.replace(each_key, ">>>" + each_key)
                if buf != newbuf:
                    print each_key
                buf = newbuf

            wf = open (os.path.join(dirname, filename), "wb")
            wf.write (buf)
            wf.close ()

configtext = '\n'.join (configs)
#print configtext
